package com.internetCafes.spms.web.customer.bizservice.impl.member;

import com.internetCafes.spms.common.CommonConstants;
import com.internetCafes.spms.core.exception.RRException;
import com.internetCafes.spms.core.exception.RRExceptionCodeEnum;
import com.internetCafes.spms.web.certificate.entity.EmployeeInfo;
import com.internetCafes.spms.web.certificate.service.EmployeeInfoService;
import com.internetCafes.spms.web.customer.bizservice.member.IMemberBizService;
import com.internetCafes.spms.web.customer.common.cont.EntMemberConst;
import com.internetCafes.spms.web.customer.model.member.MemberListReq;
import com.internetCafes.spms.web.customer.model.member.MemberListRes;
import com.sms.common.util.hutool.core.bean.BeanUtil;
import com.sms.common.util.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MemberBizServiceImpl implements IMemberBizService {

    @Autowired
    private EmployeeInfoService employeeInfoService;

    @Override
    public List<MemberListRes> list(MemberListReq req) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("status", EntMemberConst.Status.DEFAULT.getId());
        if (ObjectUtil.isNotNull(req.getType())) {
            if (EntMemberConst.MemberType.AFFILIATED.getId().equals(req.getType()) || EntMemberConst.MemberType.UN_COMPANY.getId().equals(req.getType())) {
                params.put("type", CommonConstants.EmployeeInfoType.OTHER);
            } else if (EntMemberConst.MemberType.ON_COMPANY.getId().equals(req.getType())) {
                params.put("types", new Integer[]{CommonConstants.EmployeeInfoType.TENANT_ADMIN,
                        CommonConstants.EmployeeInfoType.TENANT_CONTANT, CommonConstants.EmployeeInfoType.TENANT_Employee});
            } else {
                throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
            }
        }
        List<EmployeeInfo> memberList = employeeInfoService.query(params);

        return memberList.stream()
                .map(member -> {
                    MemberListRes res = new MemberListRes();
                    BeanUtil.copyProperties(member, res);
                    return res;
                }).collect(Collectors.toList());
    }
}
